<?php
class Search_model extends CI_Model {
	
	public function __construct() {
		parent::__construct ();
		$this->load->database();
	}
	
	public function search_blog($keywords){
		foreach ($keywords as $keyword){
			$this->db->like('CONCAT(bname)', $keyword);
		}
		$query = $this->db->get('blog');
		return $query->result_array();
	}
	
	public function search_user($keywords){
		$this->db->select('uid,uname,email,registerdate');
		foreach ($keywords as $keyword){
			$this->db->like('CONCAT(uname,email)', $keyword);
		}
		$query = $this->db->get('user');
		return $query->result_array();
	}
	
	public function search_feed($keywords){
		foreach ($keywords as $keyword){
			$this->db->like('CONCAT(type,title,content)', $keyword);
		}
		$query = $this->db->get('post');
		return $query->result_array();
	}
	
	public function search_tag($keywords){
		$sql = "select * from post where (";
		for ($i = 0 ; $i < count($keywords) ;$i++){
			$sql .= 'pid in (select pid from tag where text = "'.$keywords[$i].'")';
			if ($i < count($keywords) - 1){
				$sql .= ' and ';
			}
		}
		$sql .= ") or (";
		for ($i = 0 ; $i < count($keywords) ;$i++){
			$sql .= ' CONCAT(type,title,content) like "%'.$keywords[$i].'%"';
			if ($i < count($keywords) - 1){
				$sql .= ' and ';
			}
		}
		$sql .= ")";
		
		$query = $this->db->query($sql);
		return $query->result_array();
	}
}

?>